package com.shopx.config.service.impl;


import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.OriginException;
import com.shopx.base.exception.ShopxException;
import com.shopx.config.bean.SysCode;
import com.shopx.config.dao.db1.SysCodeDao;
import com.shopx.config.service.ISysCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * SysCode
 *
 * @author lituo 2021-01-04
 */
@Service("sysCodeService")
@Slf4j
public class SysCodeServiceImpl implements ISysCodeService,ApplicationContextAware {


  @Autowired
  private SysCodeDao sysCodeDao;



  private ApplicationContext applicationContext = null;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }


  @Override
  @Transactional(value = "db1",rollbackFor = Exception.class)
  public void testException(){
    if(1==1){
//      System.out.println(1/0);
//      throw new OriginException("99999","测试异常");
//      throw new ShopxException(ResultCodeEnum.DATA_DUPLICATE_KEY);
    }
  }



  @Override
  @Transactional(value = "db1",rollbackFor = Exception.class)
  public void testTrans(){
    SysCode sc = new SysCode();
    sc.setCodeId("111");
    sc.setCodeName("111");
    sysCodeDao.insertSelective(sc);
    SysCode sc2= new SysCode();
    sc2.setCodeId("222");
    sc2.setCodeName("111");
    sysCodeDao.insertSelective(sc2);
  }



  @Override
  @Transactional(isolation = Isolation.READ_COMMITTED,propagation= Propagation.REQUIRED)
  public void transTest(){//沿用当前事务模式。中间调用两个新开事务的方法
    log.info("transTest.begin");
    ISysCodeService codeService = applicationContext.getBean(ISysCodeService.class);//自调用失效（代理对象去调用，不会产生AOP）
    codeService.transDb1();//新事务提交
    log.info("提交");
    SysCode sysCode = new SysCode();
    sysCode.setCodeId("333");
    sysCode.setCodeName("33");
    sysCodeDao.selectByPrimaryKey(sysCode);
    SysCode sysCode2 = new SysCode();
    sysCode.setCodeId("444");
    sysCode2.setCodeName("44");
    sysCodeDao.selectByPrimaryKey(sysCode2);//沿用老事务
    System.out.println(1/0);//虽然报错，但是transDb1已经提交了
    ISysCodeService codeService2 = applicationContext.getBean(ISysCodeService.class);//自调用失效（代理对象去调用，不会产生AOP）
    codeService2.transDb2();//新事务提交
  }

  @Override
  @Transactional(isolation = Isolation.READ_COMMITTED,propagation= Propagation.REQUIRES_NEW)
  public void transDb1(){//新开一个事务
    log.info("transDb1.begin");
    SysCode sysCode = new SysCode();
    sysCode.setCodeId("111");
    sysCode.setCodeName("11");
    sysCodeDao.insertSelective(sysCode);
    SysCode sysCode2 = new SysCode();
    sysCode.setCodeId("222");
    sysCode2.setCodeName("22");
    sysCodeDao.insertSelective(sysCode2);
    log.info("transDb1.end");
  }

  @Override
  @Transactional(isolation = Isolation.READ_COMMITTED,propagation= Propagation.REQUIRES_NEW)
  public void transDb2(){//新开一个事务
    log.info("transDb1.begin");
    SysCode sysCode = new SysCode();
    sysCode.setCodeName("33");
    sysCodeDao.insertSelective(sysCode);
    SysCode sysCode2 = new SysCode();
    sysCode2.setCodeName("44");
    sysCodeDao.insertSelective(sysCode2);
    log.info("transDb1.end");
  }

  @Override
  public boolean insert(SysCode sc){
    int result = sysCodeDao.insertSelective(sc);
    if(result!=1){
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return true;
  }

}

